import axios from 'axios'

// 创建axios实例
const service = axios.create({
  baseURL: '/api/index',
  timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    // 从localStorage获取token
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['token'] = token
    }
    config.headers['Content-Type'] = 'application/json'
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    // 检查状态码
    if (res.code !== 200) {
      // 登录过期或未登录
      if (res.code === 401) {
        localStorage.removeItem('token')
        localStorage.removeItem('user')
        window.location.href = '/login'
      }
      return Promise.reject(new Error(res.msg || '请求失败'))
    }
    return res
  },
  error => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// API接口封装
export const api = {
  // 测试token是否有效
  checkToken() {
    return service.get('/checkToken')
  },
  // 注册用户
  register(data) {
    return service.post('/register', data)
  },

  // 发送手机验证码
  sendVerifyCode(data) {
    return service.post('/sendVerifyCode', data)
  },

  // 用户登录
  userLogin(data) {
    return service.post('/userLogin', data)
  },

  // 忘记密码
  forgetPassword(data) {
    return service.post('/forgetPassword', data)
  },

  // 通过设备编号获取商品信息、店铺信息和单价
  getDeviceItemInfo(params) {
    return service.get('/getDeviceItemInfo', { params })
  },

  // 创建订单
  createOrder(data) {
    return service.post('/createOrder', data)
  },

  // 订单支付
  payOrder(data) {
    return service.post('/payOrder', data)
  },

  // 获取订单详情
  getOrderDetail(params) {
    return service.post('/getOrderDetail', params)
  },
  
  // 取消订单
  cancelOrder(params) {
    return service.post('/cancelOrder', params)
  },

  // 修改密码
  changePassword(data) {
    return service.post('/changePassword', data)
  },

  // 短信验证码注册登录
  smsLoginRegister(data) {
    return service.post('/smsLoginRegister', data)
  },

  // 通过经纬度查询设备
  getDevicesByLocation(params) {
    return service.get('/getDevicesByLocation', { params })
  },
  
  // 通过经纬度查询店铺
  getShopsByLocation(params) {
    return service.get('/getShopsByLocation', { params })
  },
  
  // 获取我的订单列表
  myOrders(params) {
    return service.get('/myOrders', { params })
  }
}

// Mock数据，用于开发测试
export const mockData = {
  // 模拟设备信息
  deviceInfo: {
    device_id: 'DEV001',
    device_status: 1,
    shop: {
      id: 1,
      shop_name: '醇香酒馆',
      address: '北京市朝阳区望京SOHO T1',
      longitude: '116.48669',
      latitude: '39.99380'
    },
    item: {
      id: 1,
      name: '陈酿茅台',
      image: 'https://via.placeholder.com/300x300?text=茅台',
      info: '53度飞天茅台，口感醇厚，酱香浓郁'
    },
    price: 199,
    current_capacity: 500,
    capacity: 1000
  },
  // 模拟附近设备数据
    nearbyDevices: [
      {
        device_id: 'DEV001',
        device_status: 1,
        shop_name: '醇香酒馆',
        address: '北京市朝阳区望京SOHO T1',
        current_capacity: 500,
        capacity: 1000,
        distance: 0.5,
        distance_text: '500米'
      },
      {
        device_id: 'DEV002',
        device_status: 1,
        shop_name: '酒友小馆',
        address: '北京市海淀区中关村大街',
        current_capacity: 750,
        capacity: 1000,
        distance: 1.2,
        distance_text: '1.2公里'
      },
      {
        device_id: 'DEV003',
        device_status: 0,
        shop_name: '精品酒庄',
        address: '北京市西城区西单大街',
        current_capacity: 0,
        capacity: 1000,
        distance: 2.5,
        distance_text: '2.5公里'
      }
    ],
    
    // 模拟附近店铺数据
    nearbyShops: [
      {
        shop_id: 1,
        shop_name: '醇香酒馆',
        address: '北京市朝阳区望京SOHO T1',
        shop_status: 1,
        device_count: 5,
        distance: 0.5,
        distance_text: '500米',
        image: 'https://via.placeholder.com/150x150?text=醇香酒馆'
      },
      {
        shop_id: 2,
        shop_name: '酒友小馆',
        address: '北京市海淀区中关村大街',
        shop_status: 1,
        device_count: 3,
        distance: 1.2,
        distance_text: '1.2公里',
        image: 'https://via.placeholder.com/150x150?text=酒友小馆'
      },
      {
        shop_id: 3,
        shop_name: '精品酒庄',
        address: '北京市西城区西单大街',
        shop_status: 0,
        device_count: 8,
        distance: 2.5,
        distance_text: '2.5公里',
        image: 'https://via.placeholder.com/150x150?text=精品酒庄'
      }
    ],
  
  // 模拟订单信息
  orderDetail: {
    id: 1,
    user_id: 1,
    device_id: 'DEV001',
    item_id: 1,
    shop_id: 1,
    num: 2,
    price: 199,
    total: 398,
    status: 1,
    create_time: 1631234567,
    update_time: 1631234567,
    status_text: '已支付',
    item: {
      id: 1,
      name: '陈酿茅台',
      image: 'https://via.placeholder.com/300x300?text=茅台',
      info: '53度飞天茅台，口感醇厚，酱香浓郁'
    },
    shop: {
      id: 1,
      shop_name: '醇香酒馆',
      address: '北京市朝阳区望京SOHO T1'
    }
  },
  
  // 模拟订单列表数据
  orderList: {
    code: 200,
    msg: '查询成功',
    data: {
      total: 15,
      page: 1,
      page_size: 10,
      total_page: 2,
      list: [
        {
          id: 1,
          order_no: 'ORD20241024001',
          item_id: 1,
          item_name: '陈酿茅台',
          item_image: 'https://via.placeholder.com/300x300?text=茅台',
          shop_id: 1,
          shop_name: '醇香酒馆',
          device_id: 'DEV001',
          num: 2,
          price: 199.00,
          total: 398.00,
          status: 8,
          status_text: '完成',
          create_time: Date.now() / 1000 - 86400,
          create_time_text: '2024-10-23 10:00:00',
          update_time: Date.now() / 1000 - 86000,
          update_time_text: '2024-10-23 10:06:40',
          item: {
            id: 1,
            name: '陈酿茅台',
            image: 'https://via.placeholder.com/300x300?text=茅台',
            info: '53度飞天茅台，口感醇厚，酱香浓郁'
          },
          shop: {
            id: 1,
            shop_name: '醇香酒馆',
            address: '北京市朝阳区望京SOHO T1'
          }
        },
        {
          id: 2,
          order_no: 'ORD20241024002',
          item_id: 2,
          item_name: '五粮液',
          item_image: 'https://via.placeholder.com/300x300?text=五粮液',
          shop_id: 2,
          shop_name: '酒友小馆',
          device_id: 'DEV002',
          num: 1,
          price: 158.00,
          total: 158.00,
          status: 1,
          status_text: '已支付',
          create_time: Date.now() / 1000 - 3600,
          create_time_text: '2024-10-24 09:00:00',
          update_time: Date.now() / 1000 - 3500,
          update_time_text: '2024-10-24 09:01:40',
          item: {
            id: 2,
            name: '五粮液',
            image: 'https://via.placeholder.com/300x300?text=五粮液',
            info: '浓香型白酒，醇厚绵甜'
          },
          shop: {
            id: 2,
            shop_name: '酒友小馆',
            address: '北京市海淀区中关村大街'
          }
        },
        {
          id: 3,
          order_no: 'ORD20241024003',
          item_id: 3,
          item_name: '洋河蓝色经典',
          item_image: 'https://via.placeholder.com/300x300?text=洋河',
          shop_id: 3,
          shop_name: '精品酒庄',
          device_id: 'DEV003',
          num: 3,
          price: 128.00,
          total: 384.00,
          status: 0,
          status_text: '待支付',
          create_time: Date.now() / 1000 - 1800,
          create_time_text: '2024-10-24 09:30:00',
          update_time: Date.now() / 1000 - 1800,
          update_time_text: '2024-10-24 09:30:00',
          item: {
            id: 3,
            name: '洋河蓝色经典',
            image: 'https://via.placeholder.com/300x300?text=洋河',
            info: '绵柔型白酒，入口柔和'
          },
          shop: {
            id: 3,
            shop_name: '精品酒庄',
            address: '北京市西城区西单大街'
          }
        },
        {
          id: 4,
          order_no: 'ORD20241024004',
          item_id: 4,
          item_name: '剑南春',
          item_image: 'https://via.placeholder.com/300x300?text=剑南春',
          shop_id: 1,
          shop_name: '醇香酒馆',
          device_id: 'DEV001',
          num: 2,
          price: 168.00,
          total: 336.00,
          status: 2,
          status_text: '待打酒',
          create_time: Date.now() / 1000 - 1200,
          create_time_text: '2024-10-24 09:40:00',
          update_time: Date.now() / 1000 - 1200,
          update_time_text: '2024-10-24 09:40:00',
          item: {
            id: 4,
            name: '剑南春',
            image: 'https://via.placeholder.com/300x300?text=剑南春',
            info: '浓香型白酒，窖香浓郁'
          },
          shop: {
            id: 1,
            shop_name: '醇香酒馆',
            address: '北京市朝阳区望京SOHO T1'
          }
        },
        {
          id: 5,
          order_no: 'ORD20241024005',
          item_id: 5,
          item_name: '泸州老窖',
          item_image: 'https://via.placeholder.com/300x300?text=泸州老窖',
          shop_id: 2,
          shop_name: '酒友小馆',
          device_id: 'DEV002',
          num: 1,
          price: 138.00,
          total: 138.00,
          status: 3,
          status_text: '出酒中',
          create_time: Date.now() / 1000 - 600,
          create_time_text: '2024-10-24 09:50:00',
          update_time: Date.now() / 1000 - 300,
          update_time_text: '2024-10-24 09:55:00',
          item: {
            id: 5,
            name: '泸州老窖',
            image: 'https://via.placeholder.com/300x300?text=泸州老窖',
            info: '浓香型白酒，醇厚甘美'
          },
          shop: {
            id: 2,
            shop_name: '酒友小馆',
            address: '北京市海淀区中关村大街'
          }
        },
        {
          id: 6,
          order_no: 'ORD20241024006',
          item_id: 6,
          item_name: '汾酒',
          item_image: 'https://via.placeholder.com/300x300?text=汾酒',
          shop_id: 3,
          shop_name: '精品酒庄',
          device_id: 'DEV003',
          num: 4,
          price: 88.00,
          total: 352.00,
          status: 6,
          status_text: '部分退款',
          create_time: Date.now() / 1000 - 7200,
          create_time_text: '2024-10-24 08:00:00',
          update_time: Date.now() / 1000 - 6000,
          update_time_text: '2024-10-24 08:20:00',
          item: {
            id: 6,
            name: '汾酒',
            image: 'https://via.placeholder.com/300x300?text=汾酒',
            info: '清香型白酒，清香纯正'
          },
          shop: {
            id: 3,
            shop_name: '精品酒庄',
            address: '北京市西城区西单大街'
          }
        }
      ]
    }
  }
}

export default service